package leetcode_2401_2500;

public class LeeCode_2429 {
    public static void main(String[] args) {
//        System.out.println(minimizeXor(3, 5));
        System.out.println(minimizeXor(1, 12));
    }
    private static int minimizeXor(int num1, int num2) {
        num2 = Integer.bitCount(num2);
        int t1 = num1, c = 0;
        while (t1 != 0){
            t1 >>= 1;
            c++;
        }
        c--;
        int ans = 0;
        while (c >= 0 && num2 > 0 && num1 > 0){
            if ((num1 & (1 << c)) > 0){
                ans |= (1 << c);
                num2--;
            }
            c--;
        }
        int i = 0;
        while (num2 > 0){
            if ((ans & (1 << i)) == 0){
                ans |= 1 << i;
                num2--;
            }
            i++;
        }
        return ans;
    }
}
